<template>
  <div class="farmland-page f-c">
    <div class="f-r j-b">
      <el-form inline class="f-g">
        <el-form-item label="排序方式">
          <SwitchTabs v-model="searchParams.rank" :data="[{ name: '默认', value: ''},{ name: '创建时间', value: '2'},{ name: '面积大', value: '2'},{ name: '面积小', value: '3'}, ]" @click="onSearch" />
        </el-form-item>
        <el-form-item label="查看方式">
          <SwitchTabs v-model="way" :data="[{ name: '图标', value: '1'},{ name: '列表', value: '2'}, ]" />
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="add">新增田</el-button>
      </div>
    </div>
    <el-form inline>
      <el-form-item label="田分组">
        <el-select v-model="searchParams.fieldGroupId" clearable placeholder="">
          <el-option v-for="item in options2" :value="item.id" :label="item.groupName" />
        </el-select>
      </el-form-item>
      <el-form-item label="选择作物">
        <el-select v-model="searchParams.cropSubId" clearable placeholder="">
          <el-option v-for="item in options" :value="item.typeId" :label="item.typeName" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-left: 15px" @click="onSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <div class="border" />
    <div class="content f-g f-c">
      <div class="header f-r a-c j-b">
        <div class="f-r a-c">
          <span class="title">田块个数：</span>
          <span class="value">221块</span>
          <span class="title">田块总面积：</span>
          <span class="value">{{ fieldTotalArea }}亩</span>
        </div>
        <SwitchTabs
          v-model="componentName"
          style="margin-right: 0"
          :data="[{ name: '列表模式', value: 'FarmlandList'},{ name: '田分组', value: 'FarmlandGroup'},{ name: '地图模式', value: 'FarmlandMap'}, ]"
          @change="(val) => $emit('change', val)"
        />
      </div>
      <div v-show="way === '1'" class="row-list f-r f-w">
        <div v-for="item in tableData" :key="item.id" class="row-box">
          <div class="row-item f-c" @click="openDetail(item)">
            <img class="f-g" :src="item.img" alt="" style="margin-bottom: 53px">
            <div class="tag">{{ item.cropName }}</div>
            <div class="name-wrapper">
              <div class="name">{{ item.fieldName }}</div>
              <div class="desc">{{ item.cropName }}，{{ item.area }}亩</div>
              <el-avatar class="avatar" src="" />
            </div>
          </div>
        </div>
      </div>
      <div v-show="way === '2'" class="table-box">
        <el-table
          v-loading="loading"
          height="100%"
          stripe
          :data="tableData"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(255, 255, 255, 0.8)"
          @selection-change="onSelect"
        >
          <el-table-column type="index" align="center" label="序号" />
          <el-table-column
            v-for="item in columns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :align="item.align || 'center'"
            :min-width="item.minWidth"
          />
          <el-table-column label="操作" fixed="right" width="120px"><template slot-scope="scope">
            <el-button
              type="text"
              @click="openDetail(scope.row)"
            >查看</el-button>
            <el-button
              type="text"
              @click="onEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              @click="onDelete(scope.row)"
            >删除</el-button>
          </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-box">
        <el-pagination
          background
          :current-page="pageNo"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalSize"
          @size-change="changePageSize"
          @current-change="changePageNo"
        />
      </div>
    </div>
  </div>
</template>

<script>
import crud from '@/mixins/crud';
import { http_farmland, http_farmland_group, http_pest } from '@/api';
export default {
  name: 'FarmlandList',
  mixins: [crud],
  data() {
    return {
      request: http_farmland,
      way: '2',
      searchParams: {
        fieldGroupId: '',
        rank: '',
        cropSubId: ''
      },
      options: [],
      options2: [],
      data: [
        { value: 1, label: 'dasd' }
      ],
      tableData: [
      ],
      columns: [
        { prop: 'fieldName', label: '田块名称', minWidth: '' },
        { prop: 'cropName', label: '作物', minWidth: '' },
        { prop: '3', label: '品种', minWidth: '120px' },
        { prop: 'area', label: '面积', minWidth: '' },
        { prop: 'address', label: '地址', minWidth: '400px' },
        { prop: 'userName', label: '创建人', minWidth: '' },
        { prop: 'leader', label: '负责人', minWidth: '' },
        { prop: 'farmerPhone', label: '联系方式', minWidth: '' }
      ],
      componentName: 'FarmlandList',
      fieldTotalArea: '0.0',
      deleteKey: 'fieldId'
    }
  },
  created() {
    // 作物
    http_pest.cropList().then(res => {
      this.options = res.data
    })
    // 组
    http_farmland_group.page().then(res => {
      this.options2 = res.rows
    })
    // 田总面积
    http_farmland.totalArea({
      cropSubId: this.searchParams.cropSubId,
      fieldGroupId: this.searchParams.fieldGroupId
    }).then(res => {
      this.fieldTotalArea = res.data
    })
  },
  methods: {
    add() {
      this.$router.push({
        path: '/farmland/add'
      })
    },
    openDetail(rowItem) {
      this.$router.push({
        path: '/farmland/detail/' + rowItem[this.deleteKey],
        query: {
          fieldId: rowItem.fieldId,
          yaxis: rowItem.yaxis,
          xaxis: rowItem.xaxis,
          cropSubId: rowItem.cropSubId,
          cropId: rowItem.cropId
        }
      })
    },
    onEdit(rowItem) {
      this.$router.push({
        path: '/farmland/edit/' + rowItem.fieldId
      })
    }
  }

}
</script>

<style scoped lang="scss">
.farmland-page {
  position: relative;
  padding: 20px;
  background-color: #ffffff;
  height: 100%;
  .el-form-item {
    margin-bottom: 22px;
  }
  .border {
    border-top: 1px solid #E8E8E8;
    margin-bottom: 27px;
  }
  .row-list {
    .row-box {
      width: 20%;
      padding: 12px 10px;
      .row-item {
        cursor: pointer;
        width: 100%;
        height: 159px;
        box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
        position: relative;
        border-radius: 14px;
        background-color: #2b2f3a;
        img {
          height: calc(100% - 53px);
        }
        .tag {
          position: absolute;
          left: 11px;
          top: -5px;
          width: 36px;
          height: 16px;
          background: #1AAD23;
          line-height: 16px;
          text-align: center;
          color: #ffffff;
          font-size: 12px;
        }
        .name-wrapper {
          padding: 2px 0 0 11px;
          height: 53px;
          position: absolute;
          left: 0;
          bottom: 0;
          width: 100%;
          background-color: #ffffff;
          border-radius:0 0 14px 14px;
          overflow: hidden;
          .avatar {
            position: absolute;
            width: 35px;
            height: 35px;
            box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
            opacity: 1;
            border: 1px solid #707070;
            border-radius: 50%;
            overflow: hidden;
            top: -12px;
            right: 14px;
            background-color: #ffffff;
            z-index: 999;
          }
          .name {
            font-size: 14px;

            font-weight: 400;
            color: #595959;
            line-height: 22px;
          }
          .desc {
            margin-top: 5px;
            font-size: 12px;

            font-weight: 400;
            color: #595959;
            line-height: 22px;
          }
        }
      }
    }
  }
  .header {
    margin-bottom: 28px;
    .title {
      font-size: 16px;

      font-weight: 400;
      color: #222222;
      line-height: 24px;
    }
    .value {
      font-size: 16px;
      font-family: Source Han Sans CN-Regular, Source Han Sans CN;
      font-weight: 400;
      color: #1AAD23;
      line-height: 14px;
      margin-right: 23px;
      font-weight: bold;
    }
  }
}
</style>
